package com.elinkway.infinitemovies.async;

import android.content.Context;
import android.os.Bundle;

import com.elinkway.infinitemovies.bean.VStreamInfoList;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.http.parser.VStreamInfoListParser;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.MoviesConstant;
import com.lvideo.http.bean.LVideoDataHull;

public class RequestVStreamListTask extends MoviesHttpAsyncTask<VStreamInfoList> {


    /**
     * params = {"mid":mid, "pls":pls}
     */
    private Bundle params;
    private RequestStreamResultListener<VStreamInfoList> listener;

    public RequestStreamResultListener<VStreamInfoList> getListener() {
        return listener;
    }

    public void setListener(RequestStreamResultListener<VStreamInfoList> listener) {
        this.listener = listener;
    }

    public RequestVStreamListTask(Context context) {
        super(context);
    }

    public void setParams(Bundle params) {
        this.params = params;
    }

    @Override
    public LVideoDataHull<VStreamInfoList> doInBackground() {
        return MoviesHttpApi.requestLVideoStream(new VStreamInfoListParser(), params);
    }

    @Override
    public void onPostExecute(int updateId, VStreamInfoList result) {
        if (null != listener) {
            listener.onRequestSuccess(updateId, result);
        }

    }

    @Override
    public boolean onPreExecute() {
        if (null != listener) {
            listener.onPreRequest();
        }
        return super.onPreExecute();
    }

    @Override
    public void netNull() {
        if (null != listener) {
            LogUtils.e("PlayMediaController", "request lvideo data netNull and reason is net is unavailable");
            listener.onRequestFailed(MoviesConstant.RESPONSE_NO_NET_ERROR, "");
        }
        super.netNull();
    }

    @Override
    public void netErr(int updateId, String errMsg) {
        if (null != listener) {
            LogUtils.e("PlayMediaController", "request lvideo data netErr and reason is request exception");
            listener.onRequestFailed();
        }
        super.netErr(updateId, errMsg);
    }

    @Override
    public void dataNull(int updateId, String errMsg) {
        if (null != listener) {
            listener.onRequestFailed(MoviesConstant.RESPONSE_DATA_ERROR, errMsg);
            LogUtils.e("PlayMediaController", "request lvideo data dataNull and reason is data is null");
        }
        super.dataNull(updateId, errMsg);
    }

    @Override
    public void codeErr(String code, String msg) {
        listener.onRequestFailed(code, msg);
        super.codeErr(code, msg);
    }
}
